package com.ruoyi.application.service.impl;

import java.util.List;
import java.util.Date;
import java.util.Calendar;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.application.mapper.SysFormDataMapper;
import com.ruoyi.application.domain.SysFormData;
import com.ruoyi.application.service.ISysFormDataService;

/**
 * 表单数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-05
 */
@Service
public class SysFormDataServiceImpl implements ISysFormDataService 
{
    @Autowired
    private SysFormDataMapper sysFormDataMapper;

    /**
     * 查询表单数据
     * 
     * @param formId 表单数据主键
     * @return 表单数据
     */
    @Override
    public SysFormData selectSysFormDataByFormId(String formId)
    {
        return sysFormDataMapper.selectSysFormDataByFormId(formId);
    }

    /**
     * 查询表单数据列表
     * 
     * @param sysFormData 表单数据
     * @return 表单数据
     */
    @Override
    public List<SysFormData> selectSysFormDataList(SysFormData sysFormData)
    {
        return sysFormDataMapper.selectSysFormDataList(sysFormData);
    }

    /**
     * 新增表单数据
     * 
     * @param sysFormData 表单数据
     * @return 结果
     */
    @Override
    public int insertSysFormData(SysFormData sysFormData)
    {
        sysFormData.setCreateTime(DateUtils.getNowDate());
        return sysFormDataMapper.insertSysFormData(sysFormData);
    }

    /**
     * 修改表单数据
     * 
     * @param sysFormData 表单数据
     * @return 结果
     */
    @Override
    public int updateSysFormData(SysFormData sysFormData)
    {
        sysFormData.setUpdateTime(DateUtils.getNowDate());
        return sysFormDataMapper.updateSysFormData(sysFormData);
    }

    /**
     * 批量删除表单数据
     * 
     * @param formIds 需要删除的表单数据主键
     * @return 结果
     */
    @Override
    public int deleteSysFormDataByFormIds(String[] formIds)
    {
        return sysFormDataMapper.deleteSysFormDataByFormIds(formIds);
    }

    /**
     * 删除表单数据信息
     * 
     * @param formId 表单数据主键
     * @return 结果
     */
    @Override
    public int deleteSysFormDataByFormId(String formId)
    {
        return sysFormDataMapper.deleteSysFormDataByFormId(formId);
    }

    /**
     * 保存或更新表单数据
     * 
     * @param formId 表单ID
     * @param currentStep 当前步骤
     * @param formContent 表单内容(JSON格式)
     * @param createBy 创建者
     * @return 结果
     */
    @Override
    public int saveOrUpdateFormData(String formId, Integer currentStep, String formContent, String createBy)
    {
        SysFormData existingData = sysFormDataMapper.selectSysFormDataByFormId(formId);
        
        if (existingData != null) {
            // 更新现有数据
            existingData.setCurrentStep(currentStep);
            existingData.setFormContent(formContent);
            existingData.setUpdateBy(createBy);
            existingData.setUpdateTime(DateUtils.getNowDate());
            return sysFormDataMapper.updateSysFormData(existingData);
        } else {
            // 创建新数据
            SysFormData newData = new SysFormData();
            newData.setFormId(formId);
            newData.setCurrentStep(currentStep);
            newData.setFormContent(formContent);
            newData.setStatus(0); // 0进行中
            newData.setCreateBy(createBy);
            newData.setCreateTime(DateUtils.getNowDate());
            
            // 设置过期时间为7天后
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, 7);
            newData.setExpireTime(calendar.getTime());
            
            return sysFormDataMapper.insertSysFormData(newData);
        }
    }

    /**
     * 提交表单数据
     * 
     * @param formId 表单ID
     * @param formContent 表单内容(JSON格式)
     * @param updateBy 更新者
     * @return 结果
     */
    @Override
    public int submitFormData(String formId, String formContent, String updateBy)
    {
        SysFormData formData = sysFormDataMapper.selectSysFormDataByFormId(formId);
        if (formData != null) {
            formData.setFormContent(formContent);
            formData.setStatus(1); // 1已提交
            formData.setUpdateBy(updateBy);
            formData.setUpdateTime(DateUtils.getNowDate());
            return sysFormDataMapper.updateSysFormData(formData);
        }
        return 0;
    }
}
